package com.copm.ifm.base.service.config;

import com.alibaba.cloud.seata.web.SeataHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.validation.Validator;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * http配置类
 *
 * @author zzf
 * @date 2020/7/6 18:34
 */
@Configuration
public class HttpConfiguration extends WebMvcConfigurationSupport {

    /**
     * 跨域允许的请求方法以及header
     */
    private static final String[] ALLOW_METHODS = {"GET", "POST", "PUT", "DELETE"};
    private static final String[] ALLOW_HEADERS = {
            "Content-type",
            "accessToken",
            "Referer",
            "User-Agent",
            "Accept",
            "token",
            "Host",
            "X-Real-IP",
            "X-Forwarded-For",
            "Upgrade",
            "Connection"
    };


    /**
     * 静态资源映射
     * 不加则无法使用swagger
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        // 静态资源映射
        // 如果没有启用 @EnableWebMvc，可以配置在 application-service.yml
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");

        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/swagger-resources/**")
                .addResourceLocations("classpath:/META-INF/resources/");
    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(false) // allowedOrigins及allowCredentials不能同时为 * 和 true
                .allowedMethods(ALLOW_METHODS)
                .allowedHeaders(ALLOW_HEADERS)
                .maxAge(3600L);
    }

    /**
     * 添加拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SeataHandlerInterceptor());
        super.addInterceptors(registry);

    }


    /**
     * 暂时设置为全开
     */
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        source.registerCorsConfiguration("/**", buildConfig());

        return new CorsFilter(source);
    }

    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        // 可以自行筛选
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");

        return corsConfiguration;
    }

    @Bean
    protected Map<String, MediaType> getDefaultMediaTypes() {
        Map<String, MediaType> map = super.getDefaultMediaTypes();
        map.put("urlencoded", MediaType.APPLICATION_FORM_URLENCODED);
        map.put("all", MediaType.ALL);
        return map;
    }
}

